package code09集合.Set;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class User implements Comparable<User> {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(User user) {
        // 根据年龄进行升序排序
        if (this.age > user.getAge()) {
            return 1;
        } else if (this.age < user.getAge()) {
            return -1;
        } else {
            // 当年龄相等的时候,可以根据姓名长度进行降序排序
            if (this.name.length() > user.getName().length()) {
                return -1;
            } else if (this.name.length() < user.getName().length()) {
                return 1;
            }
            return 0;
        }
    }
}